package hash;

import java.io.IOException;
import java.util.Arrays;

import javax.print.DocFlavor.BYTE_ARRAY;

import global.PageId;

public class Hash<K, V> {
	private Object[] keys, values;
	private boolean[] tobmStones;
	private int size, n;

	public Hash(int size) {
		n = 0;
		this.size = size;
		keys = new Object[size];
		values = new Object[size];
		tobmStones = new boolean[size];
	}

	public V put(K key, V value) {
		assert n < size : "Full Size";
		int i=0;
		for (; i < size && keys[i] != null && tobmStones[i] != true; i++)
			keys[i] = key;
		values[i] = value;
		tobmStones[i] = false;
		n++;
		return value;
	}

	@SuppressWarnings("unchecked")
	public V remove(K key) {
		assert n > 0 : "Empty Hash";
		int i = 0;
		for (; i < size && keys[i] != null&&!keys[i].equals(key); i++)
			;
		if (keys[i] != null) {
			tobmStones[i] = true;
			n--;
			return (V) values[i];
		}
		return null;
	}

	public static void main(String[] args) throws IOException {
		PageId id = new PageId();
		byte[] ba = new byte[]{22,1,1,1,1,1,1,1};
		id.writeToByteArray(ba,0);
		
		System.out.println(Arrays.toString(ba));
		Hash<Integer, Integer>n=new Hash<Integer, Integer>(10);
		n.put(10, 5);
		n.put(8, 4);
		System.out.println(n.remove(8));
	}
}
